<?php
#### TEMPLATE ##################################################################
class Template {

	var $template_file;

	#### TEMPLATE ##############################################################
	function Template($template, $type = FALSE) {
		Base::getThis();

		// Check if email template exists
		if ($type == 'email')
			return $this->template_file = 'templates/email/'.$template.'.tpl';

		// Check if template exists in theme
		else if (file_exists('templates/themes/'.$this->config['theme'].'/'.$template.'.tpl'))
			return $this->template_file = 'templates/themes/'.$this->config['theme'].'/'.$template.'.tpl';

		// Check if template exists in common
		else if (file_exists('templates/common/'.$template.'.tpl'))
			return $this->template_file = 'templates/common/'.$template.'.tpl';

		// Templates resides in modules folder
		else
			return $this->template_file = 'modules/'.$this->var['module_name'].'/'.$template.'.tpl';
	}

	#### ASSIGN ################################################################
	function assign($name, $value = NULL) {
		$this->vars[$name] = $value;
	}


	#### OUTPUT ################################################################
	function output() {
		ob_start();
		if(!file_exists($this->template_file)) echo 'Template file "'.$this->template_file.'" could not be found<br />';
		else {
			$URL = $this->config['url'];
			$NAME = $this->config['name'];
			$THEME = $this->config['theme'];
			$COMMON = &$this->common;
			$CONFIG = &$this->config;
			$VAR = &$this->var;
			$ICON = &$this->icon;
			extract($this->vars);
			include($this->template_file);
		}
		$contents = ob_get_contents();
		ob_end_clean();

		return $contents;
	}

}
?>